<template>
  <div class="table-box">
    <div class="operation-box">
      <div class="input-box">
        用户名称：
        <el-input
          v-model="search"
          placeholder="请输入关键字"
          class="input__item"
        />
        <el-button type="danger">查询</el-button>
      </div>

      <div class="add-box">
        <el-button type="primary" @click="openAddDialog">
          <el-icon><CirclePlus /></el-icon>
          添加用户
        </el-button>
      </div>
    </div>

    <el-table
      :data="tableData"
      stripe
      border
      table-layout="auto"
      style="max-width: 80vw; min-width: 1000px"
    >
      <el-table-column prop="id" label="用户账号" />
      <el-table-column prop="name" label="用户名称" />
      <el-table-column prop="personal" label="角色" />
      <el-table-column prop="email" label="邮箱" />
      <el-table-column prop="lastLoginTime" label="更新时间" />
      <el-table-column prop="state" label="状态">
        <template #default="scope">
          {{ tableData[scope.$index].state ? "正常" : "冻结" }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleChange(scope.row)">
            {{ tableData[scope.$index].state ? "冻结" : "正常" }}
          </el-button>
          <el-button
            size="small"
            type="primary"
            @click="handleDelete(scope.$index, scope.row)"
          >
            修改
          </el-button>
          <el-button size="small" type="danger"> 删除 </el-button>
          <el-button size="small" type="success"> 日志 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--  -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="500"
      align-center
    >
      <el-form label-width="auto" :model="rowInfo" style="min-width: 400px">
        <el-form-item label="登录账户">
          <el-input v-model="rowInfo.id" />
        </el-form-item>
        <el-form-item label="用户名称">
          <el-input v-model="rowInfo.name" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="rowInfo.password" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="rowInfo.type" placeholder="请选择角色">
            <el-option label="系统管理员" value="1" />
            <el-option label="线路管理员" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="入职时间">
          <el-col :span="11">
            <el-date-picker
              v-model="rowInfo.joinTime"
              type="date"
              aria-label="Pick a date"
              placeholder="Pick a date"
              style="width: 100%"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="离职时间">
          <el-col :span="11">
            <el-date-picker
              v-model="rowInfo.leaveTime"
              type="date"
              aria-label="Pick a date"
              placeholder="Pick a date"
              style="width: 100%"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="使用状态">
          <el-radio-group v-model="rowInfo.state">
            <el-radio border value="Sponsor">正常</el-radio>
            <el-radio border value="Venue">冻结</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="dialogVisible = false">
            确认
          </el-button>
          <el-button type="primary" @click="dialogVisible = false">
            取消
          </el-button>
        </div>
      </template>
    </el-dialog>
    <!--  -->
    <el-dialog v-model="dialogVisible2" title="警告" width="500">
      <span>确定要冻结/恢复吗？</span>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="dialogVisible2 = false">
            确定
          </el-button>
          <el-button @click="dialogVisible2 = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";

interface TableInfo {
  date: string;
  name: string;
  state: boolean;
}

const dialogTitle = ref<"添加用户" | "修改用户">();
const search = ref("");
const dialogVisible = ref(false);
const dialogVisible2 = ref(false);

const tableData = [
  {
    id: "admin",
    name: "系统管理员",
    personal: "admin",
    email: "admin@**.com",
    lastLoginTime: "2013-12-23 13:24:14",
    state: true,
  },
  {
    id: "ps_role02",
    name: "线路管理员测试用户01",
    personal: "admin",
    email: "admin@**.com",
    lastLoginTime: "2013-12-23 13:24:14",
    state: true,
  },
  {
    id: "ps_role03",
    name: "巡检员测试用户01",
    personal: "admin",
    email: "admin@**.com",
    lastLoginTime: "2013-12-23 13:24:14",
    state: false,
  },
  {
    id: "ps_role04",
    name: "消缺员测试用户01",
    personal: "admin",
    email: "admin@**.com",
    lastLoginTime: "2013-12-23 13:24:14",
    state: false,
  },
];

const rowInfo = ref({
  id: "",
  name: "",
  personal: "",
  password: "",
  type: "",
  email: "admin@**.com",
  joinTime: "",
  leaveTime: "",
  state: true,
});

const openAddDialog = () => {
  dialogVisible.value = true;
  dialogTitle.value = "添加用户";
};

const handleChange = (row: TableInfo) => {
  dialogTitle.value = "修改用户";
  console.log(row);
  dialogVisible2.value = true;
};

const handleDelete = (index: number, row: TableInfo) => {
  console.log(index, row);
};
</script>

<style scoped>
.table-box {
  width: fit-content;
  margin: 0 auto;
}

.operation-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.input-box {
  color: #747070;
  font-size: 14px;
}

.input__item {
  width: 250px;
  margin: 10px;
}
</style>
